package com.pengtu.web.asset.message;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.User;
import com.pengtu.entity.app.UserDepartment;
import com.pengtu.entity.asset.Message.Regulations;
import com.pengtu.entity.asset.Message.RegulationsType;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.app.AccountManager;
import com.pengtu.service.app.SystemManager;
import com.pengtu.service.asset.MessageManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/asset/message")
@Result(name = BaseActionSupport.RELOAD, location = "regulations.action", type = "redirect")
/*
 * @InterceptorRefs({
 * 
 * @InterceptorRef(value="fileUpload",params={"AllowedExtensions","jpg,gif,doc,txt"
 * ,"MaximumSize","1048576"}),
 * 
 * @InterceptorRef("defaultStack") })
 */
public class RegulationsAction extends BaseActionSupport<Regulations> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public MessageManager messageManager;
	public AccountManager accountManager;
	private SystemManager systemManager;

	private String id;
	private Regulations regulation;
	private Page<Regulations> page = new Page<Regulations>(10);
	private User user;
	private Department department;
	private String regulationsType;
	private Attachment attachmention;

	public Attachment getAttachmention() {
		return attachmention;
	}

	public void setAttachmention(Attachment attachmention) {
		this.attachmention = attachmention;
	}

	public String getRegulationsType() {
		return regulationsType;
	}

	private List<Department> deptList;
	private List<RegulationsType> regulationsTypeList;

	public List<RegulationsType> getRegulationsTypeList() {
		return regulationsTypeList;
	}

	public void setRegulationsTypeList(List<RegulationsType> regulationsTypeList) {
		this.regulationsTypeList = regulationsTypeList;
	}

	public User getUser() {
		return user;
	}

	public Department getDepartment() {
		return department;
	}

	public List<Department> getDeptList() {
		return deptList;
	}

	public Page<Regulations> getPage() {
		return page;
	}

	public void setId(String id) {
		this.id = id;
	}

	@Autowired
	public void setMessageManager(MessageManager messageManager) {
		this.messageManager = messageManager;
	}

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	@Override
	public Regulations getModel() {
		// TODO Auto-generated method stub
		return regulation;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(Struts2Utils
				.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		deptList = accountManager.getDepartment();
		page = messageManager.findPageRegulations(page, queryFilter);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		user = SpringSecurityUtils.getCurrentUser();
		if ((StringUtils.isNotEmpty(id)) && !(null == user)) {
			Set<UserDepartment> deptArray = user.getUserDepartmentList();
			if (deptArray.size() > 0)
				department = deptArray.iterator().next().getDepartment();
			regulationsType = messageManager.getRegulations("id", id)
					.getRegulationType();
		}
		deptList = accountManager.getDepartment();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub

		if (StringUtils.isEmpty(id)) {
			regulation.setPublishPerson((User) SpringSecurityUtils
					.getCurrentUser());
		} else {
			regulation.setPublishPerson(regulation.getPublishPerson());
		}
		String deptId = Struts2Utils.getParameter("deptId");
		regulation.setDepartment(accountManager.getDepartment(deptId));
		messageManager.saveOrUpdateRegulation(regulation);
		addActionMessage("Title为" + regulation.getTitle() + "的制度添加成功Q@!");
		if (null != attachmention) {
			attachmention.setTableId(regulation.getId());
			attachmention.setTableField("Regulation_attachment");
			attachmention.setTableName("Regulation");
			systemManager.uploadFile(Struts2Utils.getAppRootPath(),
					attachmention);
			
			regulation.setAttachment(attachmention);
			messageManager.saveOrUpdateRegulation(regulation);
			String mes = "";
			if(StringUtils.isNotEmpty(id)){
				mes = "修改";
			}else{
				mes = "新增";
			}
			addActionMessage("Title为" + regulation.getTitle() + "的制度附件"
					+ attachmention.getUpfileFileName() + mes+"成功Q@!");
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		addActionMessage("Id为" + id + "的制度被删除Q@!");
		messageManager.deleteRegulations(id);
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			regulation = messageManager.getRegulations("id", id);
		} else {
			regulation = new Regulations();
		}
	}
	
	@Autowired
	public void setSystemManager(SystemManager systemManager) {
		this.systemManager = systemManager;
	}

}
